package com.qf.dao;

import com.qf.pojo.User;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author LiXuekai on 2022/5/27
 */
public class UserDaoImpl implements UserDao {
    //链接数据库
    private   QueryRunner queryRunner=new QueryRunner(DBUtil.getDateSource());


    @Override
    public User findAllNamed(String name) {
        String sql="SELECT * from User where name=? ";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), name);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public Integer addUser(User user) {
        String sql= "insert into tb_user (name, password, status) value(?, ?, ?)";
        int count = 0;
        try {
            count = queryRunner.update(sql, user.getName(), user.getPassword(), user.getStatus());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }


}
